package fr.liglab.adele.icasa.house.impl;

import org.apache.felix.ipojo.InstanceManager;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;

import fr.liglab.adele.icasa.device.light.BinaryLight;
import fr.liglab.adele.icasa.device.presence.PresenceSensor;
import fr.liglab.adele.icasa.device.presence.Speaker;
import fr.liglab.adele.icasa.device.temperature.Cooler;
import fr.liglab.adele.icasa.device.temperature.Heater;
import fr.liglab.adele.icasa.device.temperature.Thermometer;
import fr.liglab.adele.icasa.environment.SimulatedEnvironment;
import fr.liglab.adele.icasa.environment.SimulatedEnvironmentManager;
import fr.liglab.adele.icasa.house.abstraction.Room;
import fr.liglab.adele.icasa.house.abstraction.SoundRoom;
import fr.liglab.adele.icasa.house.factory.EnvironmentFactory;
import fr.liglab.adele.icasa.house.factory.dao.GenesisDeviceDAO;

@Component
@Instantiate
@Provides
public class LivingRoom extends SoundRoom implements Room {

	//Raquel: put the requires annotation
	@Requires(filter="(environment.id=livingroom)",optional=true)
	SimulatedEnvironment environment;
	
	InstanceManager environmentManager;
	
	@Requires(filter="(device.serialNumber=livingroom-sensor)",optional=true)
	PresenceSensor sensor;
	
	@Requires(filter="(device.serialNumber=livingroom-sound)",optional=true)
	Speaker speaker;
	
	@Requires
	SimulatedEnvironmentManager manager;
	
	@Requires
	EnvironmentFactory environmentFactory;
	
	private static final String ENVIRONMENT="livingroom";
	
	public LivingRoom(){
		getDevices().add(new GenesisDeviceDAO("light1",BinaryLight.class));
		//getDevices().add(new GenesisDeviceDAO("presense-sensor",PresenceSensor.class));
		getDevices().add(new GenesisDeviceDAO("heater",Heater.class));
		getDevices().add(new GenesisDeviceDAO("cooler",Cooler.class));
		getDevices().add(new GenesisDeviceDAO("thermometer",Thermometer.class));
	}
	
	@Validate
	public void start() throws Exception {
		environmentManager=environmentFactory.InitiateEnvironment(manager, ENVIRONMENT,getDevices());
	}

	@Invalidate
	public void stop() {
		environmentManager.dispose();
		super.deactivateDevices();
	}

	@Override
	public PresenceSensor getSensor() {	
		return this.sensor;
	}

	@Override
	public Speaker getSoundDevice() {
		return this.speaker;
	}
	
	public SimulatedEnvironment getEnvironment(){
		return environment;
	}

}
